// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      2024-05-03
// @description  try to take over the world!
// @author       You
// @match        *://*.douyin.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=douyin.com
// @grant        none
// ==/UserScript==

(function () {
  'use strict';
  console.log('████成功启动█████');


  function setupHook(xhr) {
    function getter() {
      delete xhr.responseText;  // 不delete将造成递归
      let res = xhr.responseText;
      // 是视频列表获取请求
      if (xhr.responseURL.includes('aweme/v1/web/tab/feed')) {
        let vURL = "https://5.z.wiki/autoupload/20240504/xd5d.2024-05-04_03-43-46.mp4"
        // let vURL = "https://v11-cold.douyinvod.com/d031a225b883e0f9bfc430f639163b2b/66352228/video/tos/cn/tos-cn-ve-0015c800/ogCKhAAfHI0BceQg9aAAKFUjVlYBztKBCsLyiE/?a=1128&ch=0&cr=0&dr=0&cd=0%7C0%7C0%7C0&br=1735&bt=1735&cs=0&ds=3&ft=jy_1B55QlEQp7TROxRrQE9DWy4knletl~z74C_l-Dm1Un88eK6G.yEXOf99w6x&mime_type=video_mp4&qs=0&rc=M2lnZGRkOjo0OTk3O2g4ZEBpamQ7cjQ6ZjM5ajMzNGkzM0AwM2JgXjM0NTUxL2EuXjFiYSNmYmRycjRvMi9gLS1kLS9zcw%3D%3D&btag=80010e000a0000&cquery=100y&dy_q=1714754533&feature_id=f0150a16a324336cda5d6dd0b69ed299&l=2024050400421398B03A8273E46C110270"

        let respJson = JSON.parse(xhr.response)
        console.log('█████████get responseText', respJson);

        // 遍历拿到的视频列表 进行处理
        for (let i = 0; i < respJson.aweme_list.length; i++) {
          // todo respJson.aweme_list[i].cell_room 有就是直播？    广告和直播都有 respJson.aweme_list[i].preview_video_status === 1
          // 视频超4分钟 或 是 广告  有些video是null   视频没有tag的也是广告了吧
          if (respJson.aweme_list[i].video && (respJson.aweme_list[i].video.duration > 1000 * 60 * 4 || respJson.aweme_list[i].author?.avatar_larger || !respJson.aweme_list[i].video_tag)) {
            console.log(i, '█████████描述', respJson.aweme_list[i].desc);
            respJson.aweme_list[i].desc = "ikun时长：" + respJson.aweme_list[i].video.duration
            respJson.aweme_list[i].author.nickname = respJson.aweme_list[i].video.duration // 昵称改了

            for (let ii = 0; ii < respJson.aweme_list[i].video.bit_rate.length; ii++) {
              respJson.aweme_list[i].video.bit_rate[ii].play_addr.url_list[0] = vURL
              respJson.aweme_list[i].video.bit_rate[ii].play_addr.url_list[1] = vURL
              respJson.aweme_list[i].video.bit_rate[ii].play_addr.url_list[2] = vURL
              // aweme_list[0].video.cover.url_list[0]
            }

          }
        }
        res = JSON.stringify(respJson)
      }
      setup();
      return res;
    }

    function setter(str) {
      console.log('set response: %s', str);
    }

    function setup() {
      Object.defineProperty(xhr, 'responseText', {
        get: getter,
        set: setter,
        configurable: true
      });
    }

    setup();
  }

  const oldOpen = XMLHttpRequest.prototype.open;
  XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
    if (!this._hooked) {
      this._hooked = true;
      setupHook(this);
    }
    oldOpen.apply(this, arguments);
  }
})();